<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/11/30
 * Time: 11:01
 */

namespace app\sports_admin\model;

use think\Model;
use think\db;
use CustomerType;

class Customer extends Model
{
    protected  $table = "customer";
    protected  $pk    = "id";
    protected  $hidden= ['pwd'];
    protected $autoWriteTimestamp = true;

    /**
     * 关联表将代理的身份以汉字展示
     * customer_type :关联表
    */
    public function getType()
    {
        return $this->belongsTo("customer_type","type","id");
    }
    //展示全部代理信息
    public static function showList()
    {
        return self::with("getType")->select();
    }
    //查看某个代理的信息
    public static function selectCustomer($id)
    {
        return self::with("getType")->where('id',$id)->find();
    }
    //删除代理
    public static function endCustomer($id){
        $time = time();
        return self::where("id",$id)->update(['status'=>0,"update_time"=>$time]);
    }
    //修改某个代理的信息
    public static function updateCustomer($id,$name,$phone,$tel,$remark,$email,$type){
        $time = time();
        return $result = self::where('id',$id)->update([
            "name"  =>$name,
            "phone" =>$phone,
            "tel"   =>$tel,
            "remark"=>$remark,
            "email" =>$email,
            "type"  =>$type,
            "update_time"=>$time
        ]);
    }

    /**
     * 分页展示全部代理信息
     * $adminList 展示信息
     * $adminCount 模糊度查询
     */
    public static function showListAll($page,$rows,$sort,$sortOrder,$searchName)
    {
        $adminList = self::with('getType')
            ->where(function($query)use($searchName){
                if($searchName){
                    $query->where('name|phone','like',"%{$searchName}%");
                }})
            ->where("status",1)
            ->page($page)
            ->limit($rows)
            ->order("{$sort}","{$sortOrder}")->select();

        $adminCount = self::with('getType')
            ->where(function($query)use($searchName){
                if($searchName){
                    $query->where('name|phone','like',"%{$searchName}%");
                }})
            ->where("status",1)
            ->count();
        return ['rows'=>$adminList,'total'=>$adminCount];
    }

}